package ${package}.core.manager.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import java.util.List;
<#if relation.children?exists && relation.children?size != 0>
<#list relation.children as child>
import ${package}.core.manager.${child.upperFirstTableKey}Manager;
import ${package}.core.model.${child.upperFirstTableKey};
</#list>
</#if>
import org.openbpm.base.manager.impl.BaseManager;
import ${package}.core.dao.${relation.upperFirstTableKey}Dao;
import ${package}.core.model.${relation.upperFirstTableKey};
import ${package}.core.manager.${relation.upperFirstTableKey}Manager;

<#if ( busDataPersistenceServiceBeanAPI && relation.type=='main') >
import org.openbpm.bus.api.remote.RemoteBusinessData;
import org.openbpm.bus.service.IBusinessDataPersistenceBeanService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.openbpm.base.core.util.StringUtil;
</#if>

/**
 * ${relation.tableComment} 业务逻辑层
 * @author ${author}
 * @email ${email}
 * @time ${currentDate}
 */
@Service("${relation.tableKey}Manager")
public class ${relation.upperFirstTableKey}ManagerImpl extends BaseManager<String,${relation.upperFirstTableKey}> implements ${relation.upperFirstTableKey}Manager <#if ( busDataPersistenceServiceBeanAPI && relation.type=='main')>,IBusinessDataPersistenceBeanService</#if>{
	@Resource
	${relation.upperFirstTableKey}Dao ${relation.tableKey}Dao;

	<#if relation.children?exists && relation.children?size != 0>
		<#list relation.children as child>
	@Resource
	${child.upperFirstTableKey}Manager ${child.tableKey}Manager;
		</#list>
	</#if>

<#if relation.children?exists && relation.children?size != 0>
	/**
	 * 创建实体包含子表实体
	 */
	public void create(${relation.upperFirstTableKey} ${relation.tableKey}){
		super.create(${relation.tableKey});

    	<#list relation.children as child>
		String ${genCodeUtil.getFkName(child,false)} = ${relation.tableKey}.get${genCodeUtil.getUpperFk(child,false)}();
    		<#if child.type =='oneToMany'>
		List<${child.upperFirstTableKey}> ${child.tableKey}List = ${relation.tableKey}.get${child.upperFirstTableKey}List();
		for(${child.upperFirstTableKey} ${child.tableKey}: ${child.tableKey}List){
			${child.tableKey}.set${genCodeUtil.getUpperFk(child,true)}(${genCodeUtil.getFkName(child,false)});
			${child.tableKey}Manager.create(${child.tableKey});
    	}
    		</#if>
    		<#if child.type =='oneToOne'>
    	${child.upperFirstTableKey} ${child.tableKey} = ${relation.tableKey}.get${child.upperFirstTableKey}();
    		<#-- 一对一反外键，既外键在主表上  -->
    		<#if genCodeUtil.isInversionFk(child)>
    		${child.tableKey}Manager.create(${child.tableKey});
    		${relation.tableKey}.set${genCodeUtil.getUpperFk(child,false)}(${child.tableKey}.get${genCodeUtil.getUpperFk(child,true)}());
    		super.update(${relation.tableKey});
    		<#else>
    	${child.tableKey}.set${genCodeUtil.getUpperFk(child,true)}(${genCodeUtil.getFkName(child,false)});
		${child.tableKey}Manager.create(${child.tableKey});
    		</#if>
    		</#if>
    	</#list>
    }

	/**
	 * 删除记录包含子表记录
	 */
	public void remove(String id){
		<#if relation.children?exists && relation.children?size != 0>
		${relation.upperFirstTableKey} ${relation.tableKey} = super.get(id);
		</#if>

		<#list relation.children as child>
    	${child.tableKey}Manager.delBy${genCodeUtil.getUpperFk(child,true)}(${relation.tableKey}.get${genCodeUtil.getUpperFk(child,false)}());
    	</#list>

    	super.remove(id);
	}

	/**
	 * 批量删除包含子表记录
	 */
	public void removeByIds(String [] ids){
		for(String id : ids){
			this.remove(id);
		}
	}

	/**
	 * 获取实体
	 */
    public ${relation.upperFirstTableKey} get(String id){
    	${relation.upperFirstTableKey} ${relation.tableKey} = super.get(id);

    	<#list relation.children as child>
    	${genCodeUtil.getChildClassName(child)} ${genCodeUtil.getLowFirstChildClassKey(child,false)} = ${child.tableKey}Manager.getBy${genCodeUtil.getUpperFk(child,true)}(${relation.tableKey}.get${genCodeUtil.getUpperFk(child,false)}());
    	${relation.tableKey}.set${genCodeUtil.getUpperFirstChildClassKey(child)}(${genCodeUtil.getLowFirstChildClassKey(child,false)});
    	</#list>

    	return ${relation.tableKey};
    }

    /**
     * 更新实体同时更新子表记录
     */
    public void update(${relation.upperFirstTableKey} ${relation.tableKey}){
    	super.update(${relation.tableKey});

    	<#list relation.children as child>
    	${child.tableKey}Manager.delBy${genCodeUtil.getUpperFk(child,true)}(${relation.tableKey}.get${genCodeUtil.getUpperFk(child,false)}());

   			<#if child.type =='oneToMany'>
    	List<${child.upperFirstTableKey}> ${child.tableKey}List = ${relation.tableKey}.get${child.upperFirstTableKey}List();
		for(${child.upperFirstTableKey} ${child.tableKey} : ${child.tableKey}List){
    		${child.tableKey}.set${genCodeUtil.getUpperFk(child,true)}(${relation.tableKey}.get${genCodeUtil.getUpperFk(child,false)}());
    		${child.tableKey}Manager.create(${child.tableKey});
    	}
    		</#if>
    		<#if child.type =='oneToOne'>
    	${child.upperFirstTableKey} ${child.tableKey} = ${relation.tableKey}.get${child.upperFirstTableKey}();
		${child.tableKey}Manager.create(${child.tableKey});
    		</#if>

    	</#list>
    }
	</#if>

	<#-- 提供给主表调用的子表方法 -->
	<#if relation.parent??>
	/**
	 * 通过外键删除
	 */
	public void delBy${genCodeUtil.getUpperFk(relation,true)} (String ${genCodeUtil.getFkName(relation,true)}){
		<#if relation.children?exists && relation.children?size != 0>
		List<${relation.upperFirstTableKey}> ${relation.tableKey}List = ${relation.tableKey}Dao.getBy${genCodeUtil.getUpperFk(relation,true)}(${genCodeUtil.getFkName(relation,true)});

		for(${relation.upperFirstTableKey} ${relation.tableKey} : ${relation.tableKey}List){
		<#list relation.children as child>
    		${child.tableKey}Manager.delBy${genCodeUtil.getUpperFk(child,true)}(${relation.tableKey}.get${genCodeUtil.getUpperFk(child,false)}());
    	</#list>
    	}
		</#if>

		${relation.tableKey}Dao.delBy${genCodeUtil.getUpperFk(relation,true)}(${genCodeUtil.getFkName(relation,true)});
	}



	    		<#if child.type =='oneToMany'>
		List<${child.upperFirstTableKey}> ${child.tableKey}List = ${relation.tableKey}.get${child.upperFirstTableKey}List();
		for(${child.upperFirstTableKey} ${child.tableKey}: ${child.tableKey}List){
			${child.tableKey}.set${genCodeUtil.getUpperFk(child,true)}(${genCodeUtil.getFkName(child,false)});
			${child.tableKey}Manager.create(${child.tableKey});
    	}
    		</#if>
    		<#if child.type =='oneToOne'>
    	${child.upperFirstTableKey} ${child.tableKey} = ${relation.tableKey}.get${child.upperFirstTableKey}();
		${child.tableKey}Manager.create(${child.tableKey});
    		</#if>

	<#if relation.type =='oneToMany'>
	//通过外键获取集合
	public List<${relation.upperFirstTableKey}> getBy${genCodeUtil.getUpperFk(relation,true)}(String ${genCodeUtil.getFkName(relation,true)}){
		List<${relation.upperFirstTableKey}> ${relation.tableKey}List = ${relation.tableKey}Dao.getBy${genCodeUtil.getUpperFk(relation,true)}(${genCodeUtil.getFkName(relation,true)});

		<#if relation.children?exists && relation.children?size != 0>
		for(${relation.upperFirstTableKey} ${relation.tableKey} : ${relation.tableKey}List){
		<#list relation.children as child>
    		${genCodeUtil.getChildClassName(child)} ${genCodeUtil.getLowFirstChildClassKey(child,false)} = ${child.tableKey}Manager.getBy${genCodeUtil.getUpperFk(child,true)}(${relation.tableKey}.get${genCodeUtil.getUpperFk(child,false)}());
    		${relation.tableKey}.set${genCodeUtil.getUpperFirstChildClassKey(child)}(${genCodeUtil.getLowFirstChildClassKey(child,false)});
    	</#list>
    	}
    	</#if>
		return ${relation.tableKey}List;
	}
	</#if>
	<#if relation.type =='oneToOne'>
	//通过外键获取对象
	public ${relation.upperFirstTableKey} getBy${genCodeUtil.getUpperFk(relation,true)}(String ${genCodeUtil.getFkName(relation,true)}){
		${relation.upperFirstTableKey} ${relation.tableKey} = ${relation.tableKey}Dao.getBy${genCodeUtil.getUpperFk(relation,true)}(${genCodeUtil.getFkName(relation,true)});

		<#list relation.children as child>
    		${genCodeUtil.getChildClassName(child)} ${genCodeUtil.getLowFirstChildClassKey(child,false)} = ${child.tableKey}Manager.getBy${genCodeUtil.getUpperFk(child,true)}(${relation.tableKey}.get${genCodeUtil.getUpperFk(child,false)}());
    		${relation.tableKey}.set${genCodeUtil.getUpperFirstChildClassKey(child)}(${genCodeUtil.getLowFirstChildClassKey(child,false)});
    	</#list>
		return ${relation.tableKey};
	}
	</#if>

	</#if>


	<#if ( busDataPersistenceServiceBeanAPI && relation.type=='main')>

	/**
	 * 保存 业务对象
	 * 持久化操作需要返回ID
	 * @param businessData
	 * @return id
	 */
	@Override
	public String saveData(RemoteBusinessData<JSONObject> remoteBusinessData) {
		${relation.upperFirstTableKey} ${relation.tableKey} = JSON.toJavaObject(remoteBusinessData.getData(), ${relation.upperFirstTableKey}.class) ;
		if (StringUtil.isEmpty(${relation.tableKey}.getId())) {
			this.create(${relation.tableKey});
		} else {
			this.update(${relation.tableKey});
		}
		return ${relation.tableKey}.getId();
	}
	/**
	 * 获取业务对象
	 * @param id
	 * @return  ${relation.upperFirstTableKey}
	 */
	@Override
	public RemoteBusinessData<JSONObject> loadData(RemoteBusinessData remoteBusinessData) {
		${relation.upperFirstTableKey} ${relation.tableKey} = this.get(String.valueOf(remoteBusinessData.getId()));

		RemoteBusinessData<JSONObject> data = new RemoteBusinessData<>();
		data.setData((JSONObject)JSON.toJSON(${relation.tableKey}));

		return data;
	}



	@Override
	public void removeData(RemoteBusinessData remoteBusinessData) {
		this.remove(String.valueOf(remoteBusinessData.getId()));
	}

	@Override
	public String getName() {
		return "${relation.tableKey}Manager";
	}

	</#if>
}
